package cn.edu.sgu.www.mhxysy.service.gang.impl;

import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.consts.CustomDirectories;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.gang.GangSkillCategoryDTO;
import cn.edu.sgu.www.mhxysy.entity.gang.GangSkillCategory;
import cn.edu.sgu.www.mhxysy.mapper.gang.GangSkillCategoryMapper;
import cn.edu.sgu.www.mhxysy.service.gang.GangSkillCategoryService;
import cn.edu.sgu.www.mhxysy.util.ImageUpdater;
import cn.edu.sgu.www.mhxysy.util.ObjectUtils;
import cn.edu.sgu.www.mhxysy.util.UploadUtils;
import cn.edu.sgu.www.mhxysy.util.WrapperUtils;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.function.Consumer;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class GangSkillCategoryServiceImpl implements GangSkillCategoryService {

    private final UploadUtils uploadUtils;
    private final ImageUpdater imageUpdater;
    private final GangSkillCategoryMapper gangSkillCategoryMapper;

    @Autowired
    public GangSkillCategoryServiceImpl(
            UploadUtils uploadUtils,
            ImageUpdater imageUpdater,
            GangSkillCategoryMapper gangSkillCategoryMapper) {
        this.uploadUtils = uploadUtils;
        this.imageUpdater = imageUpdater;
        this.gangSkillCategoryMapper = gangSkillCategoryMapper;
    }

    @Override
    public String upload(MultipartFile file) throws IOException {
        String directory = CustomDirectories.DIRECTORY_BPJN;

        return uploadUtils.upload(file, directory);
    }

    @Override
    public void insert(GangSkillCategoryDTO gangSkillCategoryDTO) {
        GangSkillCategory gangSkillCategory = toEntity(gangSkillCategoryDTO);

        gangSkillCategory.setId(null);

        gangSkillCategoryMapper.insert(gangSkillCategory);
    }

    @Override
    public void updateById(GangSkillCategoryDTO gangSkillCategoryDTO) {
        ObjectUtils.requireNonNull(gangSkillCategoryDTO.getId(), CustomMessages.ID_NOT_NULL);

        GangSkillCategory gangSkillCategory = toEntity(gangSkillCategoryDTO);

        gangSkillCategoryMapper.updateById(gangSkillCategory);
    }

    @Override
    public void updateImageById(Integer id, String image) {
        imageUpdater.updateImageById(image, new Consumer<Object>() {
            @Override
            public void accept(Object o) {
                UpdateWrapper<GangSkillCategory> updateWrapper = WrapperUtils.getUpdateWrapper(id, image);

                gangSkillCategoryMapper.update(updateWrapper.getEntity(), updateWrapper);
            }
        });
    }

    @Override
    public Page<GangSkillCategory> selectByPage(Pager<GangSkillCategory> pager) {
        Page<GangSkillCategory> page = Pager.ofPage(pager);

        return gangSkillCategoryMapper.selectPage(page, null);
    }

    private GangSkillCategory toEntity(GangSkillCategoryDTO gangSkillCategoryDTO) {
        GangSkillCategory gangSkillCategory = new GangSkillCategory();

        gangSkillCategory.setId(gangSkillCategoryDTO.getId());
        gangSkillCategory.setName(gangSkillCategoryDTO.getName());
        gangSkillCategory.setNote(gangSkillCategoryDTO.getNote());
        gangSkillCategory.setScore(gangSkillCategoryDTO.getScore());
        gangSkillCategory.setGrade(gangSkillCategoryDTO.getGrade());
        gangSkillCategory.setEffect(gangSkillCategoryDTO.getEffect());

        return gangSkillCategory;
    }

}